<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class LevelCompletedApiController extends AbstractRestfulController
{
    public function getList()
    {			
		header('Access-Control-Allow-Origin: *');
		
    }
    public function get($course_id)
    {		
		header('Access-Control-Allow-Origin: *');
		$data=array();		
		$courseTable=$this->getServiceLocator()->get('Models\Model\LevelCompletedFactory');
		if($course_id==0){
			$editCourse= $courseTable->getTechnologylist()->toarray();
		}else{
			$editCourse= $courseTable->editCourse($course_id)->toarray();
		}
		if(count($editCourse)!=0){
			return new JsonModel(array(
				'editCourse'  => json_encode($editCourse),
				'value' 	  => 1,
			));
		}else{
			return new JsonModel(array(
				'output'  => 'no data',
				'value'   => 0,
			));
		}
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
		$levelCompletedTable=$this->getServiceLocator()->get('Models\Model\LevelCompletedFactory');
		$checkUser = $levelCompletedTable->checkUser($data['user_id'])->toArray();
		if(count($checkUser)==0){
			$saveLevelResults = $levelCompletedTable->saveLevelResults($data);
			if($saveLevelResults!=0){
				return new JsonModel(array(
					'output' 	=> 	'success',
					'courseId'	=>	$saveLevelResults,
					'value' 	=> 	1,
				));
			}else{
				return new JsonModel(array(
					'output' => 'fail',
					'value' => 0,
				));
			}
		}else{
			$updateLevelResults = $levelCompletedTable->updateLevelResults($data);
			return new JsonModel(array(
				'output' 	=> 	'success',
				'value' 	=> 	1,
			));
		}
    }
    public function update($answer, $data)
    {
		header('Access-Control-Allow-Origin: *');	
    }
    public function delete($tech_id)
    {

    }
}